﻿#region Opis pliku
//-----------------------------------------------------------------------------
// ParticleSystem.cs
// Autor: Jan Czarnowski
// 31.10.2012
//-----------------------------------------------------------------------------
#endregion

#region Using..

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Text;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa reprezentująca zbiór emiterów
    /// oraz zarządzająca nimi.
    /// </summary>
    public class ParticleSystem
    {
        // lista emiterów
        LinkedList<Emitter> emitterList = new LinkedList<Emitter>();

        /// <summary>
        /// Liczba emiterów w danym systemie
        /// </summary>
        public int Count
        {
            get { return emitterList.Count; }
        }

        #region metody draw i update

        /// <summary>
        /// Rysuje dany system
        /// </summary>
        /// <param name="exSpriteBatch"></param>
        public void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            foreach (Emitter emitter in emitterList)
                emitter.Draw(exSpriteBatch);
        }

        /// <summary>
        /// Uaktualnienie stanu systemu
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(TimeSpan gameTime)
        {
            LinkedListNode<Emitter> node = emitterList.First;
            while (node != null)
            {
                bool isAlive = node.Value.Update(gameTime);

                node = node.Next;

                if (!isAlive)
                {
                    if (node == null)
                        emitterList.RemoveLast();
                    else
                        emitterList.Remove(node.Previous);
                }
            }
        }

        #endregion

        #region metody publiczne

        /// <summary>
        /// Metoda zwracająca string zawierający
        /// czas życia wszystkich emiterów w systemie
        /// </summary>
        public string EmittersString()
        {
            StringBuilder builder = new StringBuilder();            
            
            int i = 0;
            foreach(Emitter emitter in emitterList)
            {
                builder.AppendLine("emitter #" + i.ToString() + " time left: " + emitter.Life.ToString());
                ++i;
            }

            return builder.ToString();
        }
    
        /// <summary>
        /// Dodawanie nowego emitera do listy
        /// </summary>
        /// <param name="emitter">Obiekt typu Emitter do dodania</param>
        public void AddEmitter(Emitter emitter)
        {
            emitterList.AddLast(emitter);
        }

        #endregion
    }
}
